#include "EditorMaterialPreviewAbilityViewModel.h"

#include "Asset/EditorDefaultPackage.h"
#include "Asset/EngineDefaultPackage.h"

#include "Scene/Scene.h"
#include "Scene/StaticMeshComponent.h"
#include "Scene/DirectionalLightComponent.h"
#include "Scene/InputComponent.h"

#include "EditorMaterialPreviewAbilityModel.h"

void REditorMaterialPreviewAbilityViewModel::BindModel(TSharedObjectPtr<REditorAbilityModel> InModel)
{
    Super::BindModel(InModel);
    MaterialPreviewModel = InModel;

    Icon = REditorDefaultPackage::Get()->GetIcon(U"MaterialSymbolsPreviewSharp");
    HighlightIcon = REditorDefaultPackage::Get()->GetIcon(U"MaterialSymbolsPreviewSharp_Highlight");


    PreviewScene = NewObject<RScene>();
    {
        auto Light=PreviewScene->NewNode<RSceneNode>();
        Light->SetNodeName(U"DirectionalLight");
        Light->SetWorldPosition(SVec3(0,0,0)); //any where is ok
        Light->SetWorldRotation(SQuat::FromEuler(SVec3(CMathBase::DegreeToRadian(-45.f),0,0)));
        {
            auto LightComponent=Light->CreateAndAddComponent<RDirectionalLightComponent>();
        }

        auto GreyFloor=PreviewScene->NewNode<RSceneNode>();
        GreyFloor->SetNodeName(U"GreyFloor");
        GreyFloor->SetWorldPosition(SVec3(0,0,-1));
        GreyFloor->SetScale(SVec3(10,10,1));
        {
            auto MeshComponent=GreyFloor->CreateAndAddComponent<RStaticMeshComponent>();
            MeshComponent->SetStaticMesh(REngineDefaultPackage::Get()->GetCubeMesh());
            MeshComponent->SetMaterials({REngineDefaultPackage::Get()->GetDefaultMaterial()});
        }

        auto MeshNode=PreviewScene->NewNode<RSceneNode>();
        MeshNode->SetNodeName(U"Mesh");
        MeshNode->SetWorldPosition(SVec3(0,0,0));
        MeshNode->SetWorldRotation(SQuat::FromEuler(SVec3(0,0,0)));
        {
            auto PreviewMeshComponent=MeshNode->CreateAndAddComponent<RStaticMeshComponent>();
            PreviewMeshComponent->SetStaticMesh(REngineDefaultPackage::Get()->GetSphereMesh());
            MaterialPreviewModel->OnMaterialChanged.Bind([PreviewMeshComponent,this]()
            {
                PreviewMeshComponent->SetMaterials({this->GetSceneMaterial()});
            });
            
            PreviewMeshComponent->SetMaterials({this->GetSceneMaterial()});
        }

        PreviewScene->BeginPlay();    
    }

}

TSharedObjectPtr<RTexture2D> REditorMaterialPreviewAbilityViewModel::GetIcon() const
{
    return Icon;
}

TSharedObjectPtr<RTexture2D> REditorMaterialPreviewAbilityViewModel::GetHighlightIcon() const
{
    return HighlightIcon;
}

bool REditorMaterialPreviewAbilityViewModel::HasPreviewMaterial() const
{
    return (bool)MaterialPreviewModel->GetMaterial();
}

TSharedObjectPtr<RMaterial> REditorMaterialPreviewAbilityViewModel::GetSceneMaterial() const
{
    auto Material=MaterialPreviewModel->GetMaterial();
    if(Material)
    {
        return Material;
    }
    else
    {
        return REngineDefaultPackage::Get()->GetDefaultMaterial();
    }
}
